//
//  YDLoginView.swift
//  YDReadFish
//
//  Created by rowena on 2019/3/31.
//  Copyright © 2019年 wuyezhiguhun. All rights reserved.
//

import UIKit
import YDKeyboard

protocol YDLoginViewDataSource {
    /** 获取注册按钮的状态 */
    func registerButtonHidden() -> Bool
}

//代理 协议
protocol YDLoginViewDelegate {
    /** 登陆回调 */
    func loginViewLogin(userName: String, userPassword: String)
    /** 注册回调 */
    func loginViewRegister(userName: String, userPassword: String)
    /** 清除登陆数据回调 */
    func loginViewClearData()
}

class YDLoginView: UIView {
    
    //声明代理属性，响应协议方法
    var delegate: YDLoginViewDelegate?
    var dataSource: YDLoginViewDataSource?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        //添加View
        self.addAllViews()
        //设置所有的View的Layout
        self.addAllViewsLayout()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    

    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "fish_icon"))
        imageView.layer.cornerRadius = 10.0
        imageView.layer.masksToBounds = true
        imageView.isUserInteractionEnabled = true
        
        let recognizer = UITapGestureRecognizer.init(target: self, action: #selector(gestureRecognizerTouch))
        recognizer.numberOfTapsRequired = 3
        recognizer.numberOfTouchesRequired = 1
        imageView.addGestureRecognizer(recognizer)
        
        return imageView
    }()
    lazy var userNameField: UITextField = {
        return createTextField(placeholder: YD_USER_NAME, imageName: "login_name", type: YDKeyboardType.number, entry: false)
        
    }()
    lazy var userPasswordField: UITextField = {
        return createTextField(placeholder: YD_USER_PASSWORD, imageName: "login_password", type: YDKeyboardType.alphabet, entry: true)
        
    }()
    
    lazy var loginButton: UIButton = {
        return createButton(title: YD_LOGIN_BUTTON, backColor: UIColor.orange, action: #selector(loginButtonTouch))
        
    }()
    
    lazy var registerButton: UIButton = {
        return createButton(title: YD_REGISTER_BUTTON, backColor: UIColor.orange, action: #selector(registerButtonTouch))
    }()
    
}

// MARK: ==== 点击事件 ====
@objc extension YDLoginView {
    /** 点击登录事件 */
    func loginButtonTouch() {
        self.delegate?.loginViewLogin(userName: self.userNameField.text!, userPassword: self.userPasswordField.text!)
    }
    
    /** 点击注册事件 */
    func registerButtonTouch() {
        self.delegate?.loginViewRegister(userName: self.userNameField.text!, userPassword: self.userPasswordField.text!)
        self.registerButton.isHidden = self.dataSource!.registerButtonHidden()
    }
    
    /** 多次点击icon */
    func gestureRecognizerTouch() {
        self.delegate?.loginViewClearData()
        self.registerButton.isHidden = self.dataSource!.registerButtonHidden()
        
    }
}

// MARK: ==== 添加View ====
extension YDLoginView {
    
    /** 添加所有的View */
    func addAllViews() {
        //添加IconImageView
        self.addSubview(self.iconImageView)
        //添加用户名输入
        self.addSubview(self.userNameField)
        //添加输入密码
        self.addSubview(self.userPasswordField)
        //添加登录按钮
        self.addSubview(self.loginButton)
        //添加注册按钮
        self.addSubview(self.registerButton)
    }

}

// MARK: ==== 设置Layout ====
extension YDLoginView {
    
    /** 设置所有的View的Layout */
    func addAllViewsLayout() {
        self.addIconImageViewLayout()
        self.addUserNameFieldLayout()
        self.addUserPasswordFieldLayout()
        self.addLoginButtonLayout()
        self.addRegisterButtonLayout()
    }
    
    /**  设置IconImageView的Layout */
    func addIconImageViewLayout() {
        let topLayout = NSLayoutConstraint(item: self.iconImageView, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 100)
        let centerXLayout = NSLayoutConstraint(item: self.iconImageView, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0)
       let widthLayout = NSLayoutConstraint(item: self.iconImageView, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.width, multiplier: 1, constant: 100)
       let heightLayout = NSLayoutConstraint(item: self.iconImageView, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1, constant: 100)
        
        self.addConstraint(topLayout)
        self.addConstraint(centerXLayout)
        self.addConstraint(widthLayout)
        self.addConstraint(heightLayout)
        
        self.iconImageView.translatesAutoresizingMaskIntoConstraints = false
    }
    
    /** 设置UserNameField的Layout */
    func addUserNameFieldLayout() {
        let topLayout = NSLayoutConstraint(item: self.userNameField, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.iconImageView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 60)
        let leftLayout = NSLayoutConstraint(item: self.userNameField, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1, constant: 50)
        let rightLayout = NSLayoutConstraint(item: self.userNameField, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1, constant: -50)
        let heightLayout = NSLayoutConstraint(item: self.userNameField, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1, constant: 40)
        
        self.addConstraint(topLayout)
        self.addConstraint(leftLayout)
        self.addConstraint(rightLayout)
        self.addConstraint(heightLayout)
        
        self.userNameField.translatesAutoresizingMaskIntoConstraints = false
    }

    
    /** 设置UserPasswordField的Layout */
    func addUserPasswordFieldLayout() {
        let topLayout = NSLayoutConstraint(item: self.userPasswordField, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.userNameField, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 15)
        let leftLayout = NSLayoutConstraint(item: self.userPasswordField, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1, constant: 50)
        let rightLayout = NSLayoutConstraint(item: self.userPasswordField, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1, constant: -50)
        let heightLayout = NSLayoutConstraint(item: self.userPasswordField, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1, constant: 40)
        
        self.addConstraint(topLayout)
        self.addConstraint(leftLayout)
        self.addConstraint(rightLayout)
        self.addConstraint(heightLayout)
        
        self.userPasswordField.translatesAutoresizingMaskIntoConstraints = false
    }
    
    /** 设置LoginButton的Layout */
    func addLoginButtonLayout() {
        let topLayout = NSLayoutConstraint(item: self.loginButton, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.userPasswordField, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 50)
        let leftLayout = NSLayoutConstraint(item: self.loginButton, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1, constant: 50)
        let rightLayout = NSLayoutConstraint(item: self.loginButton, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1, constant: -50)
        let heightLayout = NSLayoutConstraint(item: self.loginButton, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1, constant: 40)
        
        self.addConstraint(topLayout)
        self.addConstraint(leftLayout)
        self.addConstraint(rightLayout)
        self.addConstraint(heightLayout)
        
        self.loginButton.translatesAutoresizingMaskIntoConstraints = false
    }
    
    /** 设置RegisterButton的Layout */
    func addRegisterButtonLayout() {
        let topLayout = NSLayoutConstraint(item: self.registerButton, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.loginButton, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 15)
        let leftLayout = NSLayoutConstraint(item: self.registerButton, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.left, multiplier: 1, constant: 50)
        let rightLayout = NSLayoutConstraint(item: self.registerButton, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.right, multiplier: 1, constant: -50)
        let heightLayout = NSLayoutConstraint(item: self.registerButton, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.height, multiplier: 1, constant: 40)
        
        self.addConstraint(topLayout)
        self.addConstraint(leftLayout)
        self.addConstraint(rightLayout)
        self.addConstraint(heightLayout)
        
        self.registerButton.translatesAutoresizingMaskIntoConstraints = false
    }

}

// MARK: ==== 创建各种View ====
extension YDLoginView {
    
    /**
     * 生成TextField
     * @pram placeholder 线上输入前提示文字
     * @pram imageName 提示图片名称
     * @pram type 弹出输入框类型
     * @pram entry 是的是密码模式
     */
    func createTextField(placeholder: String, imageName: String, type: YDKeyboardType, entry: Bool) -> UITextField {
        let textField = UITextField()
        textField.layer.cornerRadius = 5
        textField.layer.borderWidth = 1.0
        textField.layer.borderColor = UIColor.white.cgColor
        textField.yd_setKeyboardType(type)
        textField.isSecureTextEntry = entry
        textField.clearButtonMode = UITextField.ViewMode.whileEditing
        textField.font = UIFont.systemFont(ofSize: 15)
        textField.placeholder = placeholder
        
        let leftView = UIView(frame: CGRect(x: 0, y: 0, width: 35, height: 40))
        let imageView = UIImageView(image: UIImage(named: imageName))
        leftView.addSubview(imageView)
        imageView.frame = CGRect(x: 0, y: 0, width: 15, height: 15)
        imageView.center = leftView.center
        textField.leftView = leftView
        textField.leftViewMode = UITextField.ViewMode.always
        
        return textField
    }
    
    /**
     * 生成TextField
     * @pram placeholder 线上输入前提示文字
     * @pram imageName 提示图片名称
     * @pram type 弹出输入框类型
     * @pram entry 是的是密码模式
     */
    func createButton(title: String, backColor: UIColor, action: Selector) -> UIButton {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.backgroundColor = backColor
        button.layer.cornerRadius = 5.0
        button.layer.masksToBounds = true
        button.setTitle(title, for: UIControl.State.normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        button.addTarget(self, action: action, for: UIControl.Event.touchUpInside)
        
        return button
    }
}
